package com.heqiang.controller;


import com.heqiang.client.ClientServerList;
import com.heqiang.domain.Admin;

import com.heqiang.hystrix.MyHystrixCommand;
import com.heqiang.utils.DateUtils;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;


@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/get")
    public String get() {
        String s = restTemplate.getForObject("http://" + ClientServerList.SPRINGCLOUD_SERVER_PROVIDER + "/admin/get", String.class);
        return s;
    }

    /*********************************************************** get *******************************************************/
    @RequestMapping("/findOne/{id}")
    public Admin findOne(@PathVariable(value = "id") Long id) {
        String[] arr = {id.toString()};
        ResponseEntity<Admin> responseEntity = restTemplate.getForEntity("http://" + ClientServerList.SPRINGCLOUD_SERVER_PROVIDER + "/admin/findOne?id={"+id+"}", Admin.class, arr);
        return responseEntity.getBody();
    }

    @RequestMapping("/getOne/{id}")
    public Admin getOne(@PathVariable(value = "id") Long id) {
        String[] arr = {id.toString()};
        Admin admin = restTemplate.getForObject("http://" + ClientServerList.SPRINGCLOUD_SERVER_PROVIDER + "/admin/findOne?id={0}", Admin.class, arr);
        return admin;
    }

    /*********************************************************** post ******************************************************/
    @RequestMapping("/add")
    public String getOne(Admin admin) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
        map.add("account", "111111");
        map.add("password", "111111");
        map.add("phone", "15545655456");
        map.add("roleId", "4");
        map.add("createTime", DateUtils.dateToString(new Date(), DateUtils.formatStr_yyyyMMddHHmmss));
        String s = restTemplate.postForObject("http://" + ClientServerList.SPRINGCLOUD_SERVER_PROVIDER + "/admin/add", map, String.class);
        return s;
    }

    /*********************************************************** hystrtix *******************************************************/
    @RequestMapping("/hystrix")
    @HystrixCommand(fallbackMethod = "error", ignoreExceptions = RuntimeException.class, commandProperties = {
            //两秒没有返回执行回调
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    })
    public String hystrix() {
        //自定义运行时异常
        int i = 10 / 0;
        String s = restTemplate.getForObject("http://" + ClientServerList.SPRINGCLOUD_SERVER_PROVIDER + "/admin/get", String.class);
        return s;
    }

    private String error(Throwable throwable) {
        System.out.println(throwable.getMessage());
        return "失败后的熔断器回调";
    }


    //自定义请求服务熔断
    @RequestMapping("/hystrix2")
    public String hystrix2() throws ExecutionException, InterruptedException {
        MyHystrixCommand myHystrixCommand = new MyHystrixCommand(com.netflix.hystrix.HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("")), restTemplate);
        //同步调用
        //String s = myHystrixCommand.execute();
        Future<String> future = myHystrixCommand.queue();//异步调用
        //阻塞的方法,可以直接拿到结果
        String s = future.get();
        return s;//正常调用时直接返回
    }
}
